package com.softlook.mt.client.bricks.datecontrol;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.softlook.mt.client.events.ChangeDateEvents;
import com.softlook.mt.client.events.ChangeDateListener;

public class SimpleDateControl extends Composite implements ChangeDateEvents, KeyboardListener {

	AbsolutePanel m_Panel;
	Grid m_Table;
	TextBox m_from, m_to; 
	HTML m_error, m_description;
	Date m_dateBegin, m_dateEnd;
	
	private HashSet m_Listeners = new HashSet();
	
	public void addChangeDateListener(ChangeDateListener listener) {
		m_Listeners.add(listener);
	}

	public void removeChangeDateListener(ChangeDateListener listener) {
		m_Listeners.remove(listener);
	}
	
	private void notifyListeners(){
		Iterator listenerIterator = m_Listeners.iterator();
		while(listenerIterator.hasNext()){
			((ChangeDateListener)listenerIterator.next()).onChangeDate(m_dateBegin, m_dateEnd);
		}
	}
	
	public SimpleDateControl()
	{
		init();
		initWidget(m_Panel);
	}
	
	public void init()
	{
		m_Panel = new AbsolutePanel();
		m_Table = new Grid(1, 4);
	
		m_from = new TextBox();
		m_to = new TextBox();
		
		m_error = new HTML("");

		MTLocale locale = (MTLocale) GWT.create(MTLocale.class);
		m_description = new HTML(locale.dateFormat());
		//m_description = new HTML("asdasd");
		
		m_from.addKeyboardListener(this);
		m_to.addKeyboardListener(this);
		
		m_Table.setWidget(0, 0, new Label("From: "));
		m_Table.setWidget(0, 1, m_from);
		m_Table.setWidget(0, 2, new Label("To: "));
		m_Table.setWidget(0, 3, m_to);
		
		m_Panel.add(m_Table, 10, 30);
		m_Panel.add(m_error, 10, 60);
		m_Panel.add(m_description, 10, 0);
	}

	public void onKeyDown(Widget sender, char keyCode, int modifiers) {
		// TODO Auto-generated method stub
		
	}

	public void onKeyPress(Widget sender, char keyCode, int modifiers) {
		if(keyCode == KeyboardListener.KEY_ENTER)
		{
			if(m_from.getText() == "" || m_to.getText() == "")
			{
				MTLocale locale = (MTLocale) GWT.create(MTLocale.class);
				m_error.setHTML(locale.emptyField());
				//m_error.setHTML("asd");
				return;
			}
			try{
				MTLocaleMes locale = (MTLocaleMes) GWT.create(MTLocaleMes.class);
				
				m_dateBegin = parseDate(m_from.getText());
				m_dateEnd = parseDate(m_to.getText());
				m_error.setHTML(locale.fieldsFromTo(m_dateBegin.toLocaleString(), m_dateEnd.toLocaleString()));
				notifyListeners();
			}
			catch(Exception exc){
				m_error.setHTML("vrong format: " + exc.getMessage());
			}
		}
	}
	
	private Date parseDate(String strToParse) throws Exception{
		String []dateParts = strToParse.split("\\.");
		String []timeParts = strToParse.split(" ");
		if(timeParts.length > 1){
			dateParts = timeParts[0].split("\\.");
			timeParts = timeParts[1].split(":");
		}
		
		if(dateParts.length == 1){
			return new Date(Integer.parseInt(dateParts[0]) - 1900, 0, 1);
		}
		else if(dateParts.length == 2){
			return new Date(Integer.parseInt(dateParts[1]) - 1900, Integer.parseInt(dateParts[0]) - 1, 1);
		}
		else if(dateParts.length == 3){
			if(timeParts.length == 1 && timeParts[0].equalsIgnoreCase(strToParse)){
				return new Date(Integer.parseInt(dateParts[2]) - 1900, Integer.parseInt(dateParts[1]) - 1, Integer.parseInt(dateParts[0]));
			}
			else{
				if(timeParts.length == 1){
					return new Date(Integer.parseInt(dateParts[2]) - 1900, Integer.parseInt(dateParts[1]) - 1, Integer.parseInt(dateParts[0]), Integer.parseInt(timeParts[0]), 0);
				}
				else{
					return new Date(Integer.parseInt(dateParts[2]) - 1900, Integer.parseInt(dateParts[1]) - 1, Integer.parseInt(dateParts[0]), Integer.parseInt(timeParts[0]), Integer.parseInt(timeParts[1]));
				}
			}
		}
		throw new Exception(" For input string : \"" + strToParse + "\"");
	}

	public void onKeyUp(Widget sender, char keyCode, int modifiers) {
		// TODO Auto-generated method stub		
	}

}

